﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Dynamic;

namespace WorkStation.Frameworks.Core.Collections
{
    /// <summary>
    /// * 动态字典
    /// </summary>
    public class DynamicDictionary : DynamicObject
    {
        protected Dictionary<string, object> _table = new Dictionary<string, object>();

        public override IEnumerable<string> GetDynamicMemberNames()
        {
            return this._table.Keys;
        }

        protected DynamicDictionary() { }

        /// <summary>
        /// * 建造实例
        /// </summary>
        /// <returns></returns>
        public static dynamic CreateInstance() 
        {
            return new DynamicDictionary();
        }

        public override bool TryGetMember(GetMemberBinder binder, out object result)
        {
            if (this._table.Keys.Contains(binder.Name))
            {
                result = this._table[binder.Name];
                return true;
            }
            else 
            {
                result = null;
                return false;
            }
        }

        public override bool TrySetMember(SetMemberBinder binder, object value)
        {
            this._table[binder.Name] = value;
            return true;
        }
    }
}
